package aliasMatching.controller;

import aliasMatching.model.DisplayInfo;
import aliasMatching.model.MyTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rdfparser.db.DBConnection;

/**
 *
 * This class is responsible for finding the common friend between the users. It
 * gives the common friend percentage between the test user and rest of the user
 * in the list.
 *
 * @author amendrashrestha
 */
public final class FriendEquality {

    public List compareUserList;
    public List userList, username;
    public static List firstUserFriendList;
    public static List secondUserFriendList;
    int countFriend;
    EqualityActions axn;
    List commonFriends = new ArrayList();

    public FriendEquality(List userIDList, List users, MyTableModel tableMdl) throws SQLException {
        this.userList = userIDList;
        this.username = users;

        for (int i = 1; i < userList.size(); i++) {
            //for (int j = i + 1; j < userList.size(); j++) {
                commonFriends = getCommonFriend(Integer.parseInt(userList.get(0).toString()),
                        Integer.parseInt(userList.get(i).toString()));
                String common = "";
                List temp = (List) commonFriends;

                if (temp.isEmpty()) {
                    common = "0";
                } else {
                    for (Object k : temp) {
                        if (common.length() > 0) {
                            common += ",";
                        }
                        common += k;
                    }
                }
                int totalCommonFriendCount = temp.size();
                float equalPercentage;

                int selectedNodeFriends = firstUserFriendList.size() + secondUserFriendList.size() + 1;
                equalPercentage = ((totalCommonFriendCount * 100) / selectedNodeFriends);

                DisplayInfo di = new DisplayInfo(username.get(0).toString(),
                userList.get(i).toString(),
                username.get(i).toString(),
                common, equalPercentage, "Friend");
                tableMdl.SetValue(di);
            //}
        }
    }

    /**
     * Returns the list of friends to compare with our test user.
     */
    public List getAllFriends(int ID) throws SQLException {
        List friends = new ArrayList();
        PreparedStatement ps;
        ResultSet rs;
        Connection con;

        con = DBConnection.getDBConnection();
        String queryID = "select FriendBoardID from `tbl_member_friend` where BoardID = " + ID;

        try {
            ps = con.prepareStatement(queryID);
            rs = ps.executeQuery();
            while (rs.next()) {
                friends.add(rs.getInt(1));
                //System.out.println("getAllFrindsList: " +rs.getInt(1));
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return friends;
    }

    /**
     * returns common friends between the users
     */
    public List getCommonFriend(int compareID, int secondID) throws SQLException {
        firstUserFriendList = getAllFriends(compareID);
        secondUserFriendList = getAllFriends(secondID);

        List commonFriendList = new ArrayList();

        for (Object firstList : firstUserFriendList) {

            //System.out.print("i="+firstList + "\n");
            int first = Integer.parseInt(firstList.toString());

            for (Object secondList : secondUserFriendList) {

                //System.out.println("j="+secondList);
                int second = Integer.parseInt(secondList.toString());
                if (first == second) {
                    commonFriendList.add(first);
                    break;
                }
            }
        }
        return commonFriendList;
    }
}
